﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NewLife.Caching;
using NewLife.Log;
using NewLife.Reflection;
using NewLife.Serialization;

namespace redis_study
{
    class Program
    {
        static Dictionary<string, int> dic = new Dictionary<string, int>();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Redis!");

            UniqueNumber();
            return;
            //Interlock1.Run();
            //return;

            //Interlock2.Run();
            //return;

            XTrace.UseConsole();

            var _redis = new FullRedis("127.0.0.1", "", 0);
            _redis.Log = XTrace.Log;

            _redis.Set("name", "lhs");

            //expire
            _redis.Set("time", DateTime.Now, 3600);
            Console.WriteLine(_redis.Get<string>("time"));

            //实体
            var user = new User { Name = "NewLife", CreateTime = DateTime.Now };
            _redis.Set("user", user, 3600);
            var userInfo = _redis.Get<User>("user");
            Console.WriteLine(userInfo.ToJson());

            //通配符查找
            _redis.Set("user:info:name", "lhs");
            var find = _redis.Execute(null, f => f.Execute<String[]>("keys", "user:*:name"));
            Console.WriteLine("find：" + find.Join("-"));

            //字典
            var dict = _redis.GetDictionary<DateTime>("dictkey");
            dict.Add("dictvalue", DateTime.Now);
            Console.WriteLine("dict：" + dict["dictkey"]);

            //SET
            var set = _redis.GetSet<String>("setkey");
            set.Add("x");
            set.Add("y");
            set.Add("z");
            Console.WriteLine(set.Count);
            Console.WriteLine(set.Join(","));

            //列表
            var dictList = new Dictionary<string, object>
            {
                ["dict_name"] = "xx",
                ["dict_time"] = DateTime.Now,
                ["dict_age"] = 18
            };

            _redis.SetAll(dictList, 3600);
            var getDicts = _redis.GetAll<String>(dictList.Keys);
            Console.WriteLine(getDicts.Join(",", e => $"{e.Key}={e.Value}"));

            //原子操作
            var increment = _redis.Increment("count", 10);
            increment = _redis.Increment("count", 11);
            Console.WriteLine($"Increment：{increment}");

            var decrement = _redis.Decrement("count", 1);
            Console.WriteLine($"decrement：{decrement}");

            //锁
            var cacheLock = new CacheLock(_redis, "local");
            var lockAcq = cacheLock.Acquire(2000);
            //Console.WriteLine(lockAcq);

            Console.WriteLine();
            Console.WriteLine("共有缓存对象 {0} 个", _redis.Count);
        }
        class User
        {
            public String Name { get; set; }
            public DateTime CreateTime { get; set; }
        }

        static void OperationSymbol()
        {
            int int1 = 1;
            Console.WriteLine(int1 << 1);

            Console.WriteLine(int1 << 2);

            Console.WriteLine(int1 << 3);

            Console.WriteLine(int1 << 4);

            int a = 60;            /* 60 = 0011 1100 */
            int b = 13;            /* 13 = 0000 1101 */
            int c = 0;

            c = a & b;           /* 12 = 0000 1100 */
            Console.WriteLine("Line 1 - c 的值是 {0},{1}", c, Convert.ToString(c, 10));

            c = a | b;           /* 61 = 0011 1101 */
            Console.WriteLine("Line 2 - c 的值是 {0},{1}", c, Convert.ToString(c, 10));

            c = a ^ b;           /* 49 = 0011 0001 */
            Console.WriteLine("Line 3 - c 的值是 {0},{1}", c, Convert.ToString(c, 10));

            c = ~a;               /*-61 = 1100 0011 */
            Console.WriteLine("Line 4 - c 的值是 {0},{1}", c, Convert.ToString(c, 10));

            c = a << 2;     /* 240 = 1111 0000 */
            Console.WriteLine("Line 5 - c 的值是 {0},{1}", c, Convert.ToString(c, 10));

            c = a >> 2;     /* 15 = 0000 1111 */
            Console.WriteLine("Line 6 - c 的值是 {0},{1}", c, Convert.ToString(c, 10));

            //十进制转二进制 
            Console.WriteLine(Convert.ToString(69, 2));
            //十进制转八进制 
            Console.WriteLine(Convert.ToString(69, 8));
            //十进制转十六进制 
            Console.WriteLine(Convert.ToString(69, 16));

            //二进制转十进制 
            Console.WriteLine(Convert.ToInt32("100111101", 2));
            //八进制转十进制 
            Console.WriteLine(Convert.ToInt32("76", 8));
            //十六进制转十进制 
            Console.WriteLine(Convert.ToInt32("FF", 16));
        }

        static void UniqueNumber()
        {
            Console.WriteLine("UniqueNumber");
            Task.Factory.StartNew(RepeatNumber);
            Task.Factory.StartNew(RepeatNumber);
            Task.Factory.StartNew(RepeatNumber);
            Task.Factory.StartNew(RepeatNumber);
            Task.Factory.StartNew(RepeatNumber);
            Console.ReadLine();
        }
        static void RepeatNumber()
        {
            Console.WriteLine("TestRepeatNumber");
            while (true)
            {
                for (int i = 0; i < 1000000; i++)
                {
                    string time = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    dic.Add(time, 0);
                    Console.WriteLine(time);
                }
            }
        }
        
        static void Running()
        {
            Console.WriteLine("Running");
            while (true)
            {
                GUniqueCode gCode = new GUniqueCode();
                var ls = gCode.GSeqTimeCode("CEO", 20);
                //Console.WriteLine(ls.Count);
                foreach (var item in ls)
                {
                    dic.Add(item, 0);//用于检测是否成重复的id,出现重复时会出现异常
                    Console.WriteLine(item);
                }
                /// Thread.Sleep(10000);
            }
        }
    }
}
